package com.gmu.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gmu.domain.ResResult;
import com.gmu.pojo.TblDoctorAddress;
import com.gmu.service.TblDoctorAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医护人员活动地点表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@RestController
@Api(tags = "医户人员地点信息表")
@RequestMapping("/gmu/tbl-doctor-address")
public class TblDoctorAddressController {
    @Autowired
    private TblDoctorAddressService tblDoctorAddressService;

    /**
     * @param id
     * @return
     */
    @ApiOperation("由id获取该医户人员的信息")
    @GetMapping("/{id}")
    public ResResult getById(@PathVariable Integer id) {
        TblDoctorAddress doctorAddress = tblDoctorAddressService.getById(id);
        if (doctorAddress == null) {
            return new ResResult(201, "查询失败", null);
        } else {
            return new ResResult(200, "查询成功", doctorAddress);
        }
    }

    /**
     * @return
     */
    @ApiOperation("获取所有医户人员地点信息")
    @GetMapping("/getAll")
    public ResResult<?> getAll() {
        List<TblDoctorAddress> doctorAddresses = tblDoctorAddressService.list();
        if (doctorAddresses == null) {
            return new ResResult<>(201, "获取信息失败", null);
        } else {
            return new ResResult<>(200, "获取信息成功", doctorAddresses);
        }
    }

    /**
     * @param doctorAddress
     * @return
     */
    @ApiOperation("添加医户人员地点信息")
    @PostMapping("/addDoctorAddress")
    public ResResult addDoctorAddress(@RequestBody TblDoctorAddress doctorAddress) {
        boolean save = tblDoctorAddressService.save(doctorAddress);
        if (save) {
            return new ResResult(200, "添加成功");
        } else {
            return new ResResult(201, "添加失败，请重新尝试");
        }
    }

    /***
     *
     * @param doctorAddress
     * @return
     */
    @ApiOperation("修改医户人员地点信息")
    @PutMapping("/updateDoctorAddress")
    public ResResult updateDoctorAddress(@RequestBody TblDoctorAddress doctorAddress) {
        boolean update = tblDoctorAddressService.updateById(doctorAddress);
        if (update) {
            return new ResResult(200, "修改成功");
        } else {
            return new ResResult(201, "修改失败");
        }
    }

    /**
     * @param doctorAddressId
     * @return
     */
    @ApiOperation("按名删除医户人员地点信息")
    @DeleteMapping("/{doctorAddressId}")
    public ResResult DeleteDoctorAddress(@PathVariable Integer doctorAddressId) {
        boolean remove = tblDoctorAddressService.removeById(doctorAddressId);
        if (remove) {
            return new ResResult(200, "删除成功");
        } else {
            return new ResResult(201, "删除失败");
        }
    }
    /**
     * 条件查询
     */
/*    @ApiOperation("条件模糊查询")
    @GetMapping("/{currentPage}/{pageSize}")
    public ResResult getByPage(@PathVariable int currentPage, @PathVariable int pageSize, TblDoctorAddress doctorAddress) {
        IPage<TblDoctorAddress> page = tblDoctorAddressService.getByPage(currentPage, pageSize, doctorAddress);
        if (currentPage > (int) page.getPages()) {
            page = tblDoctorAddressService.getByPage((int) page.getPages(), pageSize, doctorAddress);
        }
        if (page == null) {
            return new ResResult(201, "数据有误,", null);
        } else {
            return new ResResult(200, "分页成功", page);
        }*/


    /**
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/{currentPage}/{pageSize}")
    public ResResult getPage(@PathVariable int currentPage, @PathVariable int pageSize) {
        IPage<TblDoctorAddress> page = tblDoctorAddressService.getPages(currentPage, pageSize);
        if (currentPage > (int) page.getPages()) {
            page = tblDoctorAddressService.getPages((int) page.getPages(), pageSize);
        }
        if (page == null) {
            return new ResResult(201, "数据有误,", null);
        } else {
            return new ResResult(200, "分页成功", page);
        }

    }
}

